package com.ocom.ugcapi.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.WxServiceUrl;
import com.ocom.common.entity.company.EntityFlowConfig;
import com.ocom.common.entity.human.EntityBizLeaveApply;
import com.ocom.common.request.company.*;
import com.ocom.common.request.human.LeaveApplyDetailRequest;
import com.ocom.common.request.human.NewLeaveApplyRequest;
import com.ocom.common.request.ugcapi.GzhLeaveApplyRequest;
import com.ocom.common.request.wx.AppApplyDetailRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.company.FlowConfigVo;
import com.ocom.common.vo.company.GetFlowApprovalDetailVo;
import com.ocom.common.vo.human.LeaveApplyDetailVo;
import com.ocom.common.vo.ugcapi.GzhUserInfo;
import com.ocom.ugcapi.client.CompanyClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GzhApplyController {
    @Autowired
    private CompanyClientService companyClientService;

    @PostMapping(WxServiceUrl.URL_gzhApplyList)
    public Result gzhApplyList(@RequestBody Map<String,Object> map){
        GzhUserInfo userInfo = (GzhUserInfo)map.get("userInfo");
        GetApplyListByPidRequest getApplyListByPidRequest = Convert.convert(GetApplyListByPidRequest.class,map);
        getApplyListByPidRequest.setComId(userInfo.getComId());
        getApplyListByPidRequest.setPId(userInfo.getPId());

//        GetApplyListByPidRequest getApplyListByPidRequest = new GetApplyListByPidRequest();
//        getApplyListByPidRequest.setComId(110l);
//        getApplyListByPidRequest.setPId(173338l);
        try {
            getApplyListByPidRequest.setPage((Integer)map.get("page") );
            getApplyListByPidRequest.setSize((Integer)map.get("size") );
        }catch (Exception e){

        }


        return  this.companyClientService.getApplyListByPid(getApplyListByPidRequest);
    }

    @PostMapping(WxServiceUrl.URL_gzhApplyDetail)
    public Result gzhApplyDetail(@RequestBody Map<String,Object> map){

        GzhUserInfo userInfo = (GzhUserInfo)map.get("userInfo");

        AppApplyDetailRequest applyDetailRequest  =  JSON.parseObject(JSON.toJSONString(map), AppApplyDetailRequest.class);
        // Long companyId = 110l;
        Long companyId = userInfo.getComId();
        Integer applyId = applyDetailRequest.getBusinessId();

        EntityBizLeaveApply entityBizLeaveApply=new EntityBizLeaveApply();
        entityBizLeaveApply.setId(applyId.longValue());
        entityBizLeaveApply.setComId(companyId);
        Result<EntityBizLeaveApply> leaveApplyResult = this.companyClientService.leavelApplyOne(entityBizLeaveApply);


        LeaveApplyDetailRequest leaveApplyDetailRequest = new LeaveApplyDetailRequest();
        leaveApplyDetailRequest.setId(applyId);
        leaveApplyDetailRequest.setComId(companyId);
        Result<LeaveApplyDetailVo> resultA= this.companyClientService.leaveApplyDetail(leaveApplyDetailRequest);

        GetFlowApprovalDetailRequest getFlowApprovalDetailRequest = new GetFlowApprovalDetailRequest();
        getFlowApprovalDetailRequest.setComId(companyId);
//        getFlowApprovalDetailRequest.setBusinessKey(applyDetailRequest.getActivitiKey());
        getFlowApprovalDetailRequest.setActivitiKey(leaveApplyResult.getData().getActivitiKey());
        getFlowApprovalDetailRequest.setDefKey(leaveApplyResult.getData().getDefKey());
        Result<List<GetFlowApprovalDetailVo>> getFlowApprovalDetailVoListResult= this.companyClientService.getFlowApprovalDetail(getFlowApprovalDetailRequest);

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("rawData",resultA.getData().getRawData());
        resultMap.put("formatData",resultA.getData().getFormatData());
        resultMap.put("approvalData",getFlowApprovalDetailVoListResult.getData());
        return ResultUtil.success(resultMap);
    }

    @PostMapping(WxServiceUrl.URL_gzhApplyCancel)
    public Result gzhApplyCancel(@RequestBody Map<String,Object> map){

        GzhUserInfo userInfo = (GzhUserInfo)map.get("userInfo");
//        Long companyId = 110l;
//        Long pId = 173338l;

        Long companyId = userInfo.getComId();;
        Long pId = userInfo.getPId();;
        Integer applyId = (Integer) map.get("businessId");

        EntityBizLeaveApply entityBizLeaveApply1=new EntityBizLeaveApply();
        entityBizLeaveApply1.setId(applyId.longValue());
        entityBizLeaveApply1.setComId(companyId);
        Result<EntityBizLeaveApply> leaveApplyResult = this.companyClientService.leavelApplyOne(entityBizLeaveApply1);

        String defKey=leaveApplyResult.getData().getDefKey();

        CancelApplyRequest cancelApplyRequest = new CancelApplyRequest();
        cancelApplyRequest.setComId(companyId);
        cancelApplyRequest.setDefKey(defKey);
        cancelApplyRequest.setBusinessId(applyId);
        cancelApplyRequest.setUserId(pId);
        Result cancelRes =  companyClientService.cancelApply(cancelApplyRequest);

        if (cancelRes.getCode() != 200) return cancelRes;

        GetFlowListByComRequest getFlowListByComRequest = new GetFlowListByComRequest();
        getFlowListByComRequest.setComId(companyId);
        getFlowListByComRequest.setDefKey(defKey);
        Result<EntityFlowConfig> result =  companyClientService.getFlowListByCom(getFlowListByComRequest);
        if(result.getCode()==200 && !ObjectUtil.isEmpty(result.getData())) {
            //根据 @flowKey 操作
            if(result.getData().getFlowKey().equals("leave-apply")) {
                EntityBizLeaveApply entityBizLeaveApply =new EntityBizLeaveApply();
                entityBizLeaveApply.setComId(companyId);
                entityBizLeaveApply.setId(applyId.longValue());
                entityBizLeaveApply.setStatus(3);
                Result updateRes =companyClientService.updateLeavelApply(entityBizLeaveApply);
                return updateRes;
            }
        }
        return ResultUtil.success();
    }

    @PostMapping(WxServiceUrl.URL_gzhLeaveApply)
    public Result gzhLeaveApply(@RequestBody Map<String,Object> map){
        GzhUserInfo userInfo = (GzhUserInfo)map.get("userInfo");
//        GzhUserInfo userInfo = new GzhUserInfo();
//        userInfo.setComId(110L);
//        userInfo.setPId(173338L);   //  dept 1622

        int type = (Integer) map.get("type");
        String timeS = (String)map.get("startTime");
        String timeE = (String)map.get("endTime");
        String reason = (String)map.get("reason");
        String pic = (String)map.get("pic");
        Date startTime = DateUtil.parse(timeS, "yyyy/MM/dd HH:mm:ss");
        Date endTime = DateUtil.parse(timeE,"yyyy/MM/dd HH:mm:ss");


        GzhLeaveApplyRequest request =  JSON.parseObject(JSON.toJSONString(map), GzhLeaveApplyRequest.class);
        GetFlowkeyByPidRequest getFlowkeyByPidRequest = new GetFlowkeyByPidRequest();
        getFlowkeyByPidRequest.setFlowCode("leave-apply");
        getFlowkeyByPidRequest.setComId(userInfo.getComId());
        getFlowkeyByPidRequest.setPId(userInfo.getPId().intValue());
        Result<FlowConfigVo> result3 = this.companyClientService.getFlowkeyByPid(getFlowkeyByPidRequest);
        if (result3.getCode() != 200) {
            return result3;
        }
        FlowConfigVo flowConfigVo = result3.getData();
        NewLeaveApplyRequest newLeaveApplyRequest = Convert.convert(NewLeaveApplyRequest.class,request);
        newLeaveApplyRequest.setComId(userInfo.getComId());
        newLeaveApplyRequest.setPId(userInfo.getPId());
        newLeaveApplyRequest.setStartTime(startTime);
        newLeaveApplyRequest.setEndTime(endTime);
        newLeaveApplyRequest.setPic(pic);
        newLeaveApplyRequest.setReason(reason);
        newLeaveApplyRequest.setApplyDeptId(flowConfigVo.getDeptId().longValue());
        newLeaveApplyRequest.setDays(new BigDecimal(com.ocom.common.utils.DateUtil.countWorkingDays(startTime,endTime)));
        Result<EntityBizLeaveApply> resultA = this.companyClientService.newLeaveApply(newLeaveApplyRequest);

        NewApplyRequest newApplyRequest = new NewApplyRequest();
        newApplyRequest.setComId(userInfo.getComId());
        newApplyRequest.setType(type);
        newApplyRequest.setPId(userInfo.getPId());
        newApplyRequest.setBusinessId(resultA.getData().getId());
        newApplyRequest.setApplyDeptId(flowConfigVo.getDeptId());
        newApplyRequest.setDefKey(result3.getData().getDefKey());
        newApplyRequest.setTitle(flowConfigVo.getDeptName()+flowConfigVo.getPName()+ getTypeName1(type)+"申请" );

        HashMap<String,Object> param = new HashMap<>();
        param.put("status",0);
        param.put("daiban",0);
        if(map.get("daiban")!=null ) param.put("daiban",(Integer) map.get("daiban"));
        param.put("type",type);

        newApplyRequest.setParams(param);
        newApplyRequest.setVariables(result3.getData().getVariables());
        return  this.companyClientService.newApply(newApplyRequest);
    }

    public String getTypeName1(Integer type) {
        // 类型:1-请假;2-出差;3...
        switch (type) {
            case 1:
                return "请假";
            case 2:
                return "出差";
            default:
                return "";
        }
    }
}
